00001
00002 #pragma once
00003
00004 #include "OSGEventHandler.h"
00005 #include <vector>
00006 #include "../GUIEventHandler/CommandCamera.h"
00007
00008
00009
00010 extern osg::ref_ptr<osg::Group> root;
00011
00012
00013 static bool dragFlag;
00014
00015
00016 void OSGEventHandler::calculateNewPosition(const osgGA::GUIEventAdapter& ea)
00017 {
00018
00019 osgUtil::IntersectVisitor::HitList hlist;
00020
00021 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00022 {
00023 osgUtil::Hit& hit = hlist.front();
00024
00025 new_position = hit.getWorldIntersectPoint();
00026 }
00027 }
00028
00029 void OSGEventHandler::calculateOldPosition(const osgGA::GUIEventAdapter& ea)
00030 {
00031
00032 osgUtil::IntersectVisitor::HitList hlist;
00033
00034 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00035 {
00036 osgUtil::Hit& hit = hlist.front();
00037
00038 old_position = hit.getWorldIntersectPoint();
00039 }
00040 }
00041 void OSGEventHandler::drag(TraffEduObject * dragObj,const osgGA::GUIEventAdapter& ea)
00042 {
00043 if(dragFlag==true)
00044 {
00045 osgUtil::IntersectVisitor::HitList hlist;
00046
00047 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00048 {
00049 if(ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON&&ea.getEventType()==osgGA::GUIEventAdapter::DRAG)
00050 {
00051 try
00052 {
00053 osgUtil::Hit& hit = hlist.front();
00054
00055 osg::Vec3 position = hit.getWorldIntersectPoint();
00056 position[2]=0.0;
00057 vector<TraffEduObject *> selectedObj = osgwind->getSelectedObjects();
00058 int selectedObjSize = (int)osgwind->getSelectedObjects().size();
00059
00060 if (dragObj!=NULL)
00061 {
00062 for(int i=0;i<selectedObjSize;i++)
00063 {
00064 if(selectedObj.at(i)==dragObj)
00065 {
00066 dragObj->getPAT()->setPosition(position);
00067 }
00068 else break;
00069 }
00070 }
00071
00072 }
00073 catch(...)
00074 {
00075 }
00076 }
00077 }
00078 }
00079 return;
00080 }
00081
00082
00083
00084 void OSGEventHandler::drawLight(const osgGA::GUIEventAdapter& ea)
00085 {
00086
00087 osgUtil::IntersectVisitor::HitList hlist;
00088
00089 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00090 {
00091 osgUtil::Hit& hit = hlist.front();
00092
00093 osg::Vec3 position = hit.getWorldIntersectPoint();
00094
00095 osgwind->addCommandInsertTrafficLight(position,frame->lightType,frame->lightColor);
00096 }
00097 }
00098
00099 void OSGEventHandler::drawTree(const osgGA::GUIEventAdapter& ea)
00100 {
00101
00102 osgUtil::IntersectVisitor::HitList hlist;
00103
00104 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00105 {
00106 osgUtil::Hit& hit = hlist.front();
00107
00108 osg::Vec3 position = hit.getWorldIntersectPoint();
00109
00110 osgwind->addCommandInsertEnvObj(position,"models/environmentModels/appleTree/appletree.osg",0.05);
00111 }
00112 }
00113
00114 void OSGEventHandler::drawBuilding(const osgGA::GUIEventAdapter& ea)
00115 {
00116
00117 osgUtil::IntersectVisitor::HitList hlist;
00118
00119 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00120 {
00121 osgUtil::Hit& hit = hlist.front();
00122
00123 osg::Vec3 position = hit.getWorldIntersectPoint();
00124
00125 osgwind->addCommandInsertEnvObj(position,"models/environmentModels/house3/house3.osg",0.005);
00126 }
00127 }
00128
00129
00130 void OSGEventHandler::roadPick (const osgGA::GUIEventAdapter& ea)
00131 {
00132 osgUtil::IntersectVisitor::HitList hlist;
00133
00134 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00135 {
00136 osgUtil::Hit& hit = hlist.front();
00137
00138 const osg::NodePath& nodePath = hit.getNodePath();
00139
00140 osg::Vec3 position = hit.getWorldIntersectPoint();
00141 theRoadMap->setActiveRoadIndex(theRoadMap->getLastRoadIndex());
00142 theRoadMap->addPoint (position);
00143
00144 }
00145 else ;
00146
00147 }
00148
00149 bool OSGEventHandler::drawRoad(const osgGA::GUIEventAdapter& ea)
00150 {
00151 if(ea.getEventType() == osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
00152 {
00153 if(frame->textureFlag)
00154 {
00155 roadPick(ea);
00156 return false;
00157 }
00158 return false;
00159 }
00160 else
00161 return false;
00162 }
00163
00164
00165
00166 void OSGEventHandler::drawSign(const osgGA::GUIEventAdapter& ea)
00167 {
00168 osgUtil::IntersectVisitor::HitList hlist;
00169
00170 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00171 {
00172 osgUtil::Hit& hit = hlist.front();
00173
00174 osg::Vec3 position = hit.getWorldIntersectPoint();
00175
00176 osgwind->addCommandInsertTrafficSign(position,frame->signType);
00177 }
00178
00179 }
00180
00181
00182 void OSGEventHandler::drawVehicleDodge(const osgGA::GUIEventAdapter& ea)
00183 {
00184 osgUtil::IntersectVisitor::HitList hlist;
00185
00186 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00187 {
00188 osgUtil::Hit& hit = hlist.front();
00189
00190 osg::Vec3 position = hit.getWorldIntersectPoint();
00191
00192
00193 osgwind->addCommandInsertVehicle(position,"models/carModels/dodgemax/butun.osg",0.025);
00194 }
00195
00196 }
00197 void OSGEventHandler::drawVehicleSemi(const osgGA::GUIEventAdapter& ea)
00198 {
00199 vehicleosg<<"draw semi"<<endl;
00200 osgUtil::IntersectVisitor::HitList hlist;
00201
00202 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00203 {
00204 osgUtil::Hit& hit = hlist.front();
00205
00206 osg::Vec3 position = hit.getWorldIntersectPoint();
00207
00208 osgwind->addCommandInsertVehicle(position,"models/carModels/semimax/butun.osg",0.025);
00209 }
00210
00211 }
00212
00213
00214
00215 bool OSGEventHandler::rightClickMenu(const osgGA::GUIEventAdapter& ea)
00216 {
00217 frame->m_Splitter2.SetActivePane(1,0);
00218
00219
00220 rightMenu=true;
00221
00222 frame->drawMode=SELECT;
00223 dragFlag=false;
00224
00225
00226 if((int)osgwind->getSelectedObjects().size()>0)
00227 {
00228 osgwind->addCommandDeselectAll();
00229 }
00230
00231 CMenu mnuPopupSubmit;
00232 CMenu mnuPopupSubmit2;
00233 mnuPopupSubmit2.LoadMenu(IDR_MENU2);
00234 mnuPopupSubmit.LoadMenu(IDR_MENU1);
00235
00236
00237 CPoint point;
00238
00239 GetCursorPos(&point);
00240
00241 CMenu *mnuPopupMenu2 = mnuPopupSubmit2.GetSubMenu(0);
00242 CMenu *mnuPopupMenu = mnuPopupSubmit.GetSubMenu(0);
00243
00244 ASSERT(mnuPopupMenu);
00245 ASSERT(mnuPopupMenu2);
00246
00247
00248 osgUtil::IntersectVisitor::HitList hlist;
00249
00250 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00251 {
00252 TraffEduObject *menuObject = NULL;
00253 osgUtil::IntersectVisitor::HitList::iterator hitr = hlist.begin();
00254 osg::NodePath vecNodePath = (*hitr)._nodePath;
00255 for (osg::NodePath::iterator it = vecNodePath.end()-1; it != vecNodePath.begin(); --it)
00256 {
00257 if ( (*it)->getName()== "TrafficLight" || (*it)->getName()== "TrafficSign" || (*it)->getName()== "Vehicle" || (*it)->getName()== "EnvironmentObject")
00258 {
00259 try
00260 {
00261 osgUtil::Hit& hit = hlist.front();
00262
00263 osg::Vec3 position = hit.getWorldIntersectPoint();
00264
00265 menuObject = dynamic_cast<TraffEduObject*>((*it)->getUserData());
00266 break;
00267
00268 }
00269 catch(...)
00270 {
00271
00272 }
00273 }
00274 }
00275
00276 osgUtil::Hit& hit = hlist.front();
00277
00278 osg::Vec3 position = hit.getWorldIntersectPoint();
00279
00280 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00281
00282 rightView->setOperationPosition(position);
00283
00284 if(menuObject!=NULL)
00285 {
00286
00287 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00288 rightView->setObjectInOperation(menuObject);
00289 mnuPopupMenu2->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, frame->m_Splitter2.GetPane(1,0));
00290 return true;
00291 }
00292
00293 else if(menuObject == NULL)
00294 {
00295
00296 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00297
00298 mnuPopupMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, frame->m_Splitter2.GetPane(1,0));
00299 return true;
00300 }
00301 }
00302
00303 dragFlag=false;
00304 return false;
00305
00306 }
00307
00308 void OSGEventHandler::select(const osgGA::GUIEventAdapter& ea)
00309 {
00310 TraffEduObject* pObject=NULL;
00311 osgUtil::IntersectVisitor::HitList hlist;
00312
00313 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00314 {
00315 osgUtil::IntersectVisitor::HitList::iterator hitr = hlist.begin();
00316 osg::NodePath vecNodePath = (*hitr)._nodePath;
00317 for (osg::NodePath::iterator it = vecNodePath.end()-1; it != vecNodePath.begin(); --it)
00318 {
00319 if ( (*it)->getName() == "Vehicle" || (*it)->getName()== "TrafficLight" || (*it)->getName() == "TrafficSign" || (*it)->getName()== "EnvironmentObject")
00320 {
00321 try
00322 {
00323 osgUtil::Hit& hit = hlist.front();
00324
00325 osg::Vec3 position = hit.getWorldIntersectPoint();
00326
00327 pObject= dynamic_cast<TraffEduObject*>((*it)->getUserData());
00328
00329 if (pObject!=NULL)
00330 {
00331
00332 if(ea.getModKeyMask()==osgGA::GUIEventAdapter::MODKEY_LEFT_CTRL)
00333 osgwind->addCommandSelect(1,pObject);
00334 else
00335 osgwind->addCommandSelect(0,pObject);
00336 }
00337
00338 break;
00339 }
00340 catch(...)
00341 {
00342
00343 }
00344 }
00345 }
00346 if(pObject==NULL && osgwind->getSelectedObjects().size()>0)
00347 osgwind->addCommandDeselectAll();
00348
00349 }
00350 }
00351
00352 bool OSGEventHandler::handle(const osgGA::GUIEventAdapter& ea,osgGA::GUIActionAdapter& us)
00353 {
00354
00355 if(osgwind->getCurrentFrameId()!=-1)
00356 {
00357
00358 if(defaultMouse==FALSE)
00359 {
00360
00361 if( ea.getEventType()==osgGA::GUIEventAdapter::PUSH && ea.getButton()==osgGA::GUIEventAdapter::RIGHT_MOUSE_BUTTON)
00362 {
00363 rightClickMenu(ea);
00364 osgProducer::EventAdapter::setButtonMask(0);
00365 return true;
00366 }
00367
00368 else{
00369
00370 if(ea.getEventType()==osgGA::GUIEventAdapter::RELEASE)
00371 {
00372 calculateNewPosition(ea);
00373 osgwind->addCommandTranslate(translatedObj,old_position,new_position);
00374
00375 if(frame->drawMode != DRAW_ROAD && ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
00376 {
00377 frame->drawMode=SELECT;
00378 rightMenu=false;
00379 }
00380
00381 dragFlag=false;
00382 dragObject=NULL;
00383 return true;
00384 }
00385
00386 else if (!rightMenu && ea.getEventType()==osgGA::GUIEventAdapter::DRAG)
00387 {
00388 frame->m_Splitter2.SetActivePane(1,0);
00389
00390 if(!dragFlag && ea.getEventType()==osgGA::GUIEventAdapter::DRAG &&ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
00391 {
00392 osgUtil::IntersectVisitor::HitList hlist;
00393
00394 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00395 {
00396 osgUtil::IntersectVisitor::HitList::iterator hitr = hlist.begin();
00397 osg::NodePath vecNodePath = (*hitr)._nodePath;
00398 for (osg::NodePath::iterator it = vecNodePath.end()-1; it != vecNodePath.begin(); --it)
00399 {
00400 if ( (*it)->getName()== "TrafficLight" || (*it)->getName()== "TrafficSign" || (*it)->getName()== "Vehicle" || (*it)->getName()== "EnvironmentObject")
00401 {
00402 try
00403 {
00404 osgUtil::Hit& hit = hlist.front();
00405
00406 osg::Vec3 position = hit.getWorldIntersectPoint();
00407
00408 dragObject = dynamic_cast<TraffEduObject*>((*it)->getUserData());
00409
00410 if (dragObject!=NULL)
00411 {
00412 translatedObj = dragObject;
00413 calculateOldPosition(ea);
00414
00415 dragFlag=true;
00416
00417 drag(dragObject,ea);
00418 }
00419 return true;
00420 }
00421 catch(...)
00422 {
00423
00424 }
00425 }
00426 }
00427 }
00428 }
00429
00430 else if(!rightMenu && dragFlag && ea.getEventType()==osgGA::GUIEventAdapter::DRAG && ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
00431 {
00432 drag(dragObject,ea);
00433 return true;
00434 }
00435
00436 }
00437
00438 else if (ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00439 {
00440
00441 frame->m_Splitter2.SetActivePane(1,0);
00442
00443 if(ea.getButton()==osgGA::GUIEventAdapter::LEFT_MOUSE_BUTTON)
00444 {
00445 DrawMode mode = frame->drawMode;
00446
00447 switch(mode)
00448 {
00449 case DRAW_VEHICLE_DODGE:
00450 {
00451 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00452 {
00453 drawVehicleDodge(ea);
00454 return true;
00455 }
00456
00457 }
00458 case DRAW_VEHICLE_SEMI:
00459 {
00460 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00461 {
00462 drawVehicleSemi(ea);
00463 return true;
00464 }
00465
00466 }
00467
00468 case DRAW_TRAFFIC_SIGN:
00469 {
00470 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00471 {
00472 drawSign(ea);
00473 return true;
00474 }
00475
00476 }
00477
00478 case DRAW_TRAFFIC_LIGHT:
00479 {
00480
00481 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00482 {
00483 drawLight(ea);
00484 return true;
00485 }
00486
00487 }
00488
00489 case DRAW_ROAD:
00490 {
00491 drawRoad(ea);
00492 return true;
00493 }
00494
00495 case DRAW_TREE:
00496 {
00497
00498 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00499 {
00500 drawTree(ea);
00501 return true;
00502 }
00503 }
00504
00505 case DRAW_HOUSE:
00506 {
00507
00508 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00509 {
00510 drawBuilding(ea);
00511 return true;
00512 }
00513
00514 }
00515
00516 case SELECT:
00517 {
00518 if(ea.getEventType()==osgGA::GUIEventAdapter::PUSH)
00519 {
00520 select(ea);
00521 return true;
00522 }
00523
00524 }
00525
00526 }
00527
00528 }
00529 return false;
00530 }
00531
00532
00533
00534 else if (ea.getEventType()==osgGA::GUIEventAdapter::KEYDOWN)
00535 {
00536
00537 frame->m_Splitter2.SetActivePane(1,0);
00538 if(ea.getKey() == 'C' || ea.getKey()=='c')
00539 {
00540
00541 defaultMouse=TRUE;
00542 }
00543
00544 if(ea.getKey() == 'o' || ea.getKey() == 'O')
00545 {
00546
00547 osgUtil::IntersectVisitor::HitList hlist;
00548
00549 if (_viewer->computeIntersections(ea.getX(),ea.getY(),hlist))
00550 {
00551 osgUtil::Hit& hit = hlist.front();
00552
00553 osg::Vec3 position = hit.getWorldIntersectPoint();
00554
00555 osgwind->addCommandInsertTrafficLight(position,NORMAL_ISIK,YESIL,YESIL);
00556 }
00557 }
00558
00559 if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Right)
00560 {
00561 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00562 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00563 cmdCamera->MoveRight();
00564
00565 }
00566
00567 if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Left)
00568 {
00569
00570 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00571 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00572 cmdCamera->MoveLeft();
00573 }
00574 if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Up)
00575 {
00576 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00577 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00578 cmdCamera->MoveUp();
00579 }
00580 if(ea.getKey() == osgGA::GUIEventAdapter::KEY_Down)
00581 {
00582
00583 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00584 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00585 cmdCamera->MoveDown();
00586 }
00587 if(ea.getKey() == 'z')
00588 {
00589 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00590 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00591 cmdCamera->ZoomIn();
00592 }
00593 if(ea.getKey() == 'x')
00594 {
00595
00596 RightView * rightView = dynamic_cast <RightView *> (frame->m_Splitter2.GetPane(1,0));
00597 CommandCamera *cmdCamera = new CommandCamera(rightView,osgwind);
00598 cmdCamera->ZoomOut();
00599 }
00600
00601 }
00602 }
00603 }
00604
00605 else if(defaultMouse == TRUE)
00606 {
00607 switch(ea.getEventType())
00608 {
00609 case osgGA::GUIEventAdapter::KEYDOWN:
00610 {
00611 if(ea.getKey() == 'P' || ea.getKey()=='p')
00612 {
00613 defaultMouse=FALSE;
00614 }
00615 break;
00616 }
00617 }
00618 }
00619 return false;
00620 }
00621 return false;
00622 }
00623